package com.hbsx.why.views;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hbsx.why.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by CC on 2016/6/5.
 */

public class TopBar extends Toolbar {

    @BindView(R.id.btn_back)
    public
    BackButton backButton;

    @BindView(R.id.tv_topTitle)
    public
    TextView topTitle;

    @BindView(R.id.btn_top_right)
    ImageView btnTopRight;
    @BindView(R.id.text_top_right)
    TextView textTopRight;
    String title;

    public TopBar(Context context) {
        super(context);
        init();
    }

    public TopBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TopBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setContentInsetsAbsolute(0, 0);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        boolean hasChild = getChildCount() != 0;
        if (!hasChild) {
            LayoutInflater.from(getContext()).inflate(R.layout.layout_toolbar, this, true);
        } else {
            View contentView = LayoutInflater.from(getContext()).inflate(R.layout.layout_toolbar, this, false);
            int position = getTitle() == null ? 0 : 1;
            if (!(getChildAt(position) instanceof ViewGroup)) {
                throw new IllegalArgumentException("TopBar的子元素必须是一个ViewGroup");
            }
            if (getChildCount() != 0) {
                ViewGroup view = (ViewGroup) getChildAt(position);
                view.addView(contentView, 0);
            }
        }
        ButterKnife.bind(this);
        if (!TextUtils.isEmpty(title)) {
            topTitle.setText(title);
        } else {
            if (isInEditMode()) {
                topTitle.setText(getContext().getString(R.string.app_name));
            } else {
                topTitle.setText(((Activity) getContext()).getTitle());
            }
        }
        if (getBackground() != null) {
            ((View) backButton.getParent()).setBackgroundDrawable(getBackground());
        }
    }

    public void setBtnTopRight(ImageView btnTopRight) {
        this.btnTopRight = btnTopRight;
    }

    @Override
    public void setTitle(@StringRes int resId) {
        if (topTitle != null)
            topTitle.setText(getResources().getString(resId));
    }

    @Override
    public void setTitle(CharSequence title) {
        this.title = title.toString();
        if (topTitle != null) {
            topTitle.setText(title);
        }
    }

    public void setBtnTopRightIcon(@DrawableRes int resId) {
        btnTopRight.setImageResource(resId);
    }


    public ImageView getTopRight() {
        return btnTopRight;
    }

    public void setBtnTopRightClickListener(OnClickListener clickListener) {
        btnTopRight.setVisibility(VISIBLE);
        btnTopRight.setOnClickListener(clickListener);
    }

    /**
     * 右边的textview
     *
     * @param sourceid      0不传   图片的id 可为空
     * @param textRight     text显示的文字
     * @param clickListener 点击监听
     */
    public void setTextTopRight(int sourceid, String textRight, OnClickListener clickListener) {
        textTopRight.setVisibility(VISIBLE);
        if (sourceid != 0) {
            Drawable nav_up = getResources().getDrawable(sourceid);
            nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
            textTopRight.setCompoundDrawables(nav_up, null, null, null);
        }
        textTopRight.setText(textRight);
        textTopRight.setOnClickListener(clickListener);
    }

    public BackButton getBackButton() {
        return backButton;
    }

    public void setBackIcon(int resId) {
        backButton.setImageResource(resId);
    }

    public void setBackButtonListener(OnClickListener onClickListener) {
        backButton.setOnClickListener(onClickListener);
    }

}
